import axios from 'axios'
import { message } from 'antd'

const isPrd = process.env.NODE_ENV === 'production'
export const basciUrl = isPrd ? '' : ''
const service = axios.create({
  baseURL: basciUrl
})

service.interceptors.request.use(config => {
  if (config.needFormdata) {
    config.headers = {
      'Content-Type': 'multipart/form-data; boundary=----WebKitFormBoundarynl6gT1BKdPWIejNq',
      tokenUf: sessionStorage.getItem('token') || ''
    }
  } else {
    config.headers = {
      'Content-Type': 'application/json;charset=utf-8',
      tokenUf: sessionStorage.getItem('token') || ''
    }
  }
  return config
}, error => {
  return error
})

service.interceptors.response.use(response => {
  if (response.data.code) {
    switch (response.data.code) {
      case 200:
        return response.data
      case 302:
        message.warning('接口重定向了！')
        return Promise.reject(response.data.msg || 'Error')
      case 400:
        setTimeout(() => {
          location.replace('')
        }, 1500);
        return Promise.reject(message.warning(response.data.msg) || 'Error')
      case 401:
        message.error('您未登录，或者登录已经超时，请先登录！')
        return Promise.reject(response.data.msg || 'Error')
      case 403:
        message.error('无权限操作')
        return Promise.reject(response.data.msg || 'Error')
      case 500:
        message.error(response.data.msg)
        return Promise.reject(response.data.msg || 'Error')
      default:
        message.error(response.data.msg)
        return Promise.reject(response.data.msg || 'Error')
    }
  } else {
    return response
  }
})
export default service
